package com.ruoyi.quartz.task;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.service.impl.TaskService;
import com.ruoyi.system.V2.financial.mapper.FinanceMapper;
import com.ruoyi.system.V2.financial.service.IFinanceStatisticsService;
import com.ruoyi.system.V2.inter.mapper.SelectPushMapper;
import com.ruoyi.system.model.domain.Finance;
import com.ruoyi.system.model.domain.FinanceStatistics;
import com.ruoyi.system.model.domain.SelectPush;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling
@EnableAsync
public class AddTask {

    @Autowired
    private TaskService taskService;
    @Resource
    private SelectPushMapper selectPushMapper;
    @Autowired
    private FinanceMapper financeMapper;
    @Autowired
    private IFinanceStatisticsService financeStatisticsService;

    //添加定时任务
    //标签同步
    private void synchronousLable() {
        log.info("标签同步接口");
        taskService.synchronousLable();
    }


    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPushTaskceshi() {
        SelectPush selectPush = new SelectPush();
        selectPush.setState("0");
        selectPush.setType("ceshi");
        List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
        excuteTask(selectPushes);
    }

    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPush1Task() {
        SelectPush selectPush = new SelectPush();
        selectPush.setState("0");
        selectPush.setType("1");
        List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
        excuteTask(selectPushes);
    }


    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPush2Task() {
        SelectPush selectPush = new SelectPush();
        selectPush.setState("0");
        selectPush.setType("2");
        List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
        excuteTask(selectPushes);
    }


    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPush3Task() {
        SelectPush selectPush = new SelectPush();
        selectPush.setState("0");
        selectPush.setType("3");
        List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
        excuteTask(selectPushes);
    }


    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPush4Task() {
        SelectPush selectPush = new SelectPush();
        selectPush.setState("0");
        selectPush.setType("4");
        List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
        excuteTask(selectPushes);
    }


    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPush5Task() {
        SelectPush selectPush = new SelectPush();
        selectPush.setState("0");
        selectPush.setType("5");
        List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
        excuteTask(selectPushes);
    }

    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPushTask6() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("6");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush push : selectPushes) {
                taskService.selectPush(5, push);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }


    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPushTask7881() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("7881");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush push : selectPushes) {
                taskService.selectPush(1, push);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/2 * * * ?")
    private void selectPushTaskPanzhi() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("panzhi");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush push : selectPushes) {
                taskService.selectPush(1, push);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

//    @Scheduled(cron = "0 0/2 * * * ?")
//    private void selectPushTaskPriceP1() {
//        try {
//            SelectPush selectPush = new SelectPush();
//            selectPush.setState("0");
//            selectPush.setType("P1");
//            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
//            for (SelectPush push : selectPushes) {
//                taskService.selectPush(1, push, Integer.parseInt(selectPush.getSmPrice(), selectPush.getBigPrice());
//            }
//        } catch (RuntimeException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//
//    @Scheduled(cron = "0 0/2 * * * ?")
//    private void selectPushTaskPriceP2() {
//        try {
//            SelectPush selectPush = new SelectPush();
//            selectPush.setState("0");
//            selectPush.setType("P2");
//            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
//            for (SelectPush push : selectPushes) {
//                taskService.selectPush(1, push, 800, 99999);
//            }
//        } catch (RuntimeException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Scheduled(cron = "0/5 * * * * ?")
//    private void selectPushTaskPriceP3() {
//        try {
//            SelectPush selectPush = new SelectPush();
//            selectPush.setState("0");
//            selectPush.setType("P3");
//            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
//            for (SelectPush push : selectPushes) {
//                taskService.selectPush(10, push, 500, 99999);
//            }
//        } catch (RuntimeException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Scheduled(cron = "0/5 * * * * ?")
//    private void selectPushTaskPriceP4() {
//        try {
//            SelectPush selectPush = new SelectPush();
//            selectPush.setState("0");
//            selectPush.setType("P4");
//            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
//            for (SelectPush push : selectPushes) {
//                taskService.selectPush(10, push, 369, 99999);
//            }
//        } catch (RuntimeException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Scheduled(cron = "0/5 * * * * ?")
//    private void selectPushTaskPriceP5() {
//        try {
//            SelectPush selectPush = new SelectPush();
//            selectPush.setState("0");
//            selectPush.setType("P5");
//            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
//            for (SelectPush push : selectPushes) {
//                taskService.selectPush(10, push, 300, 99999);
//            }
//        } catch (RuntimeException e) {
//            throw new RuntimeException(e);
//        }
//    }

    @Scheduled(cron = "0 0/5 * * * ?")
    private void selectPushTaskPanzhPangxie() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("pangxie");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush push : selectPushes) {
                taskService.selectPush(1, push);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    private void selectPushTaskqufenPangxie() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("qufenpangxie");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush push : selectPushes) {
                taskService.selectPush2(1, push);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    private void selectPushTasksjzPangxie() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("sjzpangxie");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush push : selectPushes) {
                taskService.selectPushsjzpx(1, push);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0 23 * * ?")
    private void selectPushTaskStopPanzhi() {
        SelectPush selectPush = new SelectPush();
        selectPush.setState("0");
        selectPush.setType("panzhi");
        List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
        for (SelectPush push : selectPushes) {
            push.setType("111100000");
            selectPushMapper.updateSelectPush(push);
        }
    }


    @Scheduled(cron = "0 0/2 * * * ?")
    private void selectPushTaskKL() {
        try {

            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("2");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            excuteTask(selectPushes);
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    private void selectPushTaskJYM() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("JYM");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush selectPush1 : selectPushes) {
                taskService.selectPush(1, selectPush1);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    private void selectPushTaskJYMS() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("JYMS");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush selectPush1 : selectPushes) {
                taskService.selectPush(1, selectPush1);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0/10 * * * * ?")
    private void selectPushTaskYSY() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("YSY");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush selectPush1 : selectPushes) {
                taskService.selectPush(1, selectPush1);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0/20 * * * * ?")
    private void selectPushTaskZYL() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("ZYL");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush selectPush1 : selectPushes) {
                taskService.selectPush(1, selectPush1);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    private void selectPushTaskCLL() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("4");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            excuteTask(selectPushes);
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/2 * * * ?")
    private void selectPushTaskPANGXIE() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("houpangxie");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush selectPush1 : selectPushes) {
                taskService.selectPushPangXie(5, selectPush1);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/2 * * * ?")
    private void selectPushTaskqufuHOUPANGXIE() {
        try {

            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("qufuhoupangxie");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush selectPush1 : selectPushes) {
                taskService.selectPushPangXie2(5, selectPush1);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Scheduled(cron = "0 0/2 * * * ?")
    private void selectPushTasksjzHOUPANGXIE() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("sjzhoupangxie");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush selectPush1 : selectPushes) {
                taskService.selectPushPangXiesjzpx(5, selectPush1);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    private void excuteTask(List<SelectPush> selectPushes) {
        try {
            for (SelectPush selectPush : selectPushes) {
                taskService.selectPush(10, selectPush);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }

    }

//    //螃蟹token
//    @Scheduled(cron = "0 0/10 * * * ?")
//    private void pushPANGXIEGetTokenApi() {
//        taskService.pushPANGXIEGetTokenApi();
//    }

    @Scheduled(cron = "0/20 * * * * ?")
    public void selectPushTaskPangxie() {
        try {
            SelectPush selectPush = new SelectPush();
            selectPush.setState("0");
            selectPush.setType("rengong");
            List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
            for (SelectPush push : selectPushes) {
                taskService.selectPush(1, push);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }

//    @Scheduled(cron = "0 0 0/20 * * ?")
//    private void pushJINGYUGetTokenApi() {
//        taskService.pushJINGYUGetTokenApi(70l);
//    }

    //标签同步
//    @Scheduled(cron = "0 0 0/50 * * ?")
//    private void pushCHANGYOUGetTokenApi() {
//        taskService.pushCHUANGYOUGetTokenApi(66l);
//    }
//
//    @Scheduled(cron = "0 0/5 * * * ?")
//    private void pushMONIMIMAGetTokenApi() {
//
//        taskService.pushMONIMIMAGetTokenApi();
//    }
//
//    @Scheduled(cron = "0 0/5 * * * ?")
//    private void pushYISHOUYOUGetTokenApi() {
//
//        taskService.pushYISHOUYOUGetTokenApi();
//    }
//
//    @Scheduled(cron = "0 0/10 * * * ?")
//    private void pushMONI7881GetTokenApi() {
//
//        taskService.pushMONI7881GetTokenApi();
//    }
//
//
//    //淘号号token
//    @Scheduled(cron = "0 0/20 * * * ?")
//    private void pushTAOHAOHAOGetTokenApi() {
//        taskService.pushTAOHAOHAOGetTokenApi();
//    }

    @Scheduled(cron = "0 0 3 * * ?")
    //标签同步
    private void delAll() {

        taskService.delAll();
    }

    @Scheduled(cron = "1 20 00 * * ?")
    private void recycleStatistics() {
        Finance finance = new Finance();
        finance.setDay(1);
        Map<String, Object> result = financeMapper.getData(finance);
        String shouru = null;
        String zhichu = null;
        if (StringUtils.isNotNull(result)) {
            shouru = String.valueOf(result.get("shouru"));
            zhichu = String.valueOf(result.get("zhichu"));
        }
        FinanceStatistics financeStatistics = new FinanceStatistics();
        financeStatistics.setCreateId(1l);
        financeStatistics.setStatisticsDate(DateUtils.getBeforDate(1));
        if (StringUtils.isNotEmpty(shouru)) {
            financeStatistics.setIncomePrice(new BigDecimal(shouru));
        } else {
            financeStatistics.setIncomePrice(new BigDecimal("0"));
        }
        if (StringUtils.isNotEmpty(zhichu)) {
            financeStatistics.setDisbursePrice(new BigDecimal(zhichu));
        } else {
            financeStatistics.setDisbursePrice(new BigDecimal("0"));
        }
        financeStatistics.setProfit(financeStatistics.getIncomePrice().subtract(financeStatistics.getDisbursePrice()));
        financeStatisticsService.save(financeStatistics);
    }

}
